home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / freeWAIS-sf-1.1 / ir / list.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-19  |  3.7 KB  |  228 lines

  1. /* WIDE AREA INFORMATION SERVER SOFTWARE
  2.    No guarantees or restrictions.  See the readme file for the full standard
  3.    disclaimer.    
  4.    Brewster@think.com
  5. */
  6.  
  7. /* Copyright (c) CNIDR (see ../COPYRIGHT) */
  8.  
  9.  
  10. /* Change log:
  11.  * $Log: list.c,v $
  12.  * Revision 1.2  1994/03/08  21:06:31  pfeifer
  13.  * Patchlevel 04
  14.  *
  15.  * Revision 1.1  1993/02/16  15:05:35  freewais
  16.  * Initial revision
  17.  *
  18.  * Revision 1.4  92/03/07  19:44:00  jonathan
  19.  * changed function argument to sort_list from long to int.
  20.  * mycroft@hal.gnu.ai.mit.edu.
  21.  * 
  22.  * Revision 1.3  92/02/13  12:40:11  jonathan
  23.  * Corrected syntax for function as arguments.
  24.  * 
  25.  * Revision 1.2  92/02/12  13:33:36  jonathan
  26.  * Added "$Log" so RCS will put the log message in the header
  27.  * 
  28. */
  29.  
  30. /* List Utilities.
  31.  *
  32.  * -brewster
  33.  */
  34.  
  35.  
  36. #include "list.h"
  37.  
  38. /* a list is an end_of_list terminated array */
  39.  
  40. char empty_tmp = 'f';
  41. void *end_of_list = (void*)&empty_tmp;  /* this is special */
  42.  
  43. void* car(list)
  44. void **list;
  45. {
  46.   if(end_of_list == *list)
  47.     return(end_of_list);
  48.   else
  49.     return(*list);
  50. }
  51.  
  52. /* returns the nth element (0 is the first element).
  53.   it returns NULL if it asks for an element off the end (?).
  54.  */
  55.  
  56. void* nth(number, list)
  57. long number;
  58. void **list;
  59. {
  60.   if(number < length(list))
  61.     return(list[number]);
  62.   else
  63.     return(NULL); 
  64. }
  65.  
  66. void 
  67. setf_nth(number, elem, list)
  68. long number;
  69. void* elem;
  70. void**list;
  71. /* set the nth element */
  72. {
  73.   if(number < length(list))
  74.     list[number] = elem;
  75. }
  76.  
  77. void* first(list)
  78. void **list;
  79. {
  80.   return(car(list));
  81. }
  82.  
  83. void* second(list)
  84. void **list;
  85. {
  86.   return(car(cdr(list)));
  87. }
  88.  
  89. void* last(list)
  90. void **list;
  91. {
  92.   long len = length(list);
  93.   if (len > 0)
  94.     return(nth(len - 1,list));
  95.   else
  96.     return(NULL);
  97. }
  98.  
  99. void** cdr(list)
  100. void **list;{
  101.   if(NULL == list)
  102.     return((void**)end_of_list);
  103.   else if(end_of_list == *list)
  104.     return((void**)end_of_list);
  105.   else
  106.     return(list+1);
  107. }
  108.  
  109. void** nth_cdr(list, n)
  110. void **list;
  111. long n;{
  112.   void** l = list;
  113.   long i;
  114.   for (i = 0; i < n; i++)
  115.     l = cdr(l);
  116.   return(l);
  117. }
  118.  
  119. void** rest(list)
  120. void **list;
  121. {
  122.   return(cdr(list));
  123. }
  124.  
  125. void* cadr(list)
  126. void **list;
  127. {
  128.   if(NULL == list)
  129.     return(NULL);
  130.   else if(end_of_list == *list)
  131.     return(NULL);
  132.   else
  133.     return(*(list+1));
  134. }
  135.  
  136. /* length of a list.  returns -1 if error.*/
  137. long length(list)
  138. void** list;
  139. {
  140.   long count = 0;
  141.   if(list == NULL)
  142.     return(0);
  143.   while(end_of_list != list[count])
  144.     count++;
  145.   return(count);
  146. }
  147.  
  148. void mapcar(list, function)
  149. void **list;
  150. void (*function)();
  151. {
  152.   if(!null(list)){
  153.     (*function)(car(list));
  154.     mapcar(cdr(list), function);
  155.   }
  156. }
  157.  
  158. /* pushes the item on the end of the list. returns the list. */
  159. void **collecting(list, item)
  160. void **list;
  161. void *item;
  162. {
  163.   long len = length(list);
  164.   if(0 == len){
  165.     list = (void**)malloc(2 * sizeof(void*));
  166.     list[0] = item;
  167.     list[1] = end_of_list;
  168.     return(list);
  169.   }
  170.   else{
  171.     list = (void **)realloc((char *)list, (len + 2) * sizeof(void*));
  172.     list[len] = item;
  173.     list[len + 1] = end_of_list;
  174.   }
  175.   return(list);
  176. }
  177.  
  178.  
  179. void setf_car(list, item)
  180. void** list;
  181. void* item;
  182. {
  183.   list[0] = item;
  184. }
  185.  
  186. /* returns true if the list is NULL */
  187. boolean null(list)
  188. void **list;
  189. {
  190.   if(list == NULL)
  191.     return(true);
  192.   if(list[0] == end_of_list)
  193.     return(true);
  194.   return(false);
  195. }
  196.  
  197. boolean free_list(list)
  198. void **list;
  199. {
  200.   if(list != NULL)
  201.     s_free(list);
  202.   return(true);
  203. }
  204.  
  205. void
  206. sort_list(list, cmp)
  207. void** list;
  208. int (*cmp)();
  209.   qsort(list,length(list),sizeof(void*),cmp);
  210. }
  211.  
  212. void**
  213. remove_item_from_list(list, pos)
  214. void** list;
  215. long pos;
  216. {
  217.   long count = pos;
  218.   if (list == NULL || list[0] == end_of_list)
  219.     return(NULL);
  220.   while (end_of_list != list[count])
  221.    { list[count] = list[count + 1];
  222.      count++;
  223.    }
  224.   list = (void**)realloc((char*)list,count*sizeof(void*));
  225.   return(list);
  226. }
  227.